<template>
  <div class="search">
    <!-- form 可以然后ios的键盘由发送变为搜索  -->
    <form action="/">
      <van-search
      background="#3296FA"
        v-model="value"
        show-action
        placeholder="请输入搜索关键词"
        @search="onSearch"
        @cancel="$router.back()"
        @focus="isShow=false"
      />
    </form>
    <search-results :seatchvalue='value'  v-if="isShow"/>

    <search-history @clear='history=[]' @onSearch='onSearch' :history='history' v-else-if="!value"/>
    <search-suggestion  @onsearch='onSearch' :searchText='value' v-else/>
  </div>
</template>

<script>
import SearchHistory from './components/SearchHistory.vue'
import SearchResults from './components/SearchResults.vue'
import SearchSuggestion from './components/SearchSuggestion.vue'
import { setStorage, getStorage } from '@/utils/storage.js'
export default {
  components: { SearchHistory, SearchSuggestion, SearchResults },
  name: 'search',
  data () {
    return {
      value: '',
      isShow: false,
      history: getStorage('TT-HISTORY') || []
    }
  },

  created () {},

  methods: {
    onSearch (val) {
      this.value = val
      this.isShow = true
      this.history.unshift(val)
      this.history = [...new Set(this.history)]
    },
    onCancel () {

    }
    // suggestion (e) {
    //   console.log(e)
    //   this.value = e
    // }
  },
  watch: {
    history: {
      deep: true,
      handler (val) {
        setStorage('TT-HISTORY', val)
      }
    }
  }
}
</script>

<style scoped lang="less">
.search {
  .van-search__action{
    color: #fff;
  }
}
</style>
